package com.dongge.install;

import com.dongge.entity.Role;
import com.dongge.entity.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.HashSet;
import java.util.Set;

/**
 * Created by zhudong on 2016/8/18.
 */
public class InitializationClass {
    public static void init(){
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("db-init.xml");
        SessionFactory sessionFactory = (SessionFactory)applicationContext.getBean("sessionFactory");
        Session session = sessionFactory.openSession();
        Transaction tx = session.beginTransaction();
        User admin = new User();
        admin.setUsername("admin");
        admin.setPassword("123456");
        Role role = new Role();
        role.setName("admin");
        Set<User> userSet = new HashSet<User>();
        userSet.add(admin);
        role.setUsers(userSet);
        session.save(role);
        tx.commit();
        session.close();
    }
    public static void main(String[] args) {
        init();
    }
}
